package se.boars.dailystatistics;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class resultPage extends Activity{
	
	TextView text1;
	TextView text2;
	TextView text3;
	TextView text4;
	TextView text5;
	int questionId;
	int answer_a;
	int answer_b;
	int answer_c;
	int total_count;
	ProgressDialog dialog;
	View view;
	JSONParser jParser;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resultpage);
		dialog = new ProgressDialog(this);
		text1 = (TextView)findViewById(R.id.textView5);
		text2 = (TextView)findViewById(R.id.textView6);
		text3 = (TextView)findViewById(R.id.textView7);
		text4 = (TextView)findViewById(R.id.textView8);
		text5 = (TextView)findViewById(R.id.textView10);
		jParser = new JSONParser();
	}
	@Override
	protected void onStart() {
		super.onStart();
		new getResultTask().execute();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		new getResultTask().execute();
	}
	
	private class getResultTask extends AsyncTask<Void, Void, Void> {
		
		@Override
		protected void onPreExecute() {
			dialog.setMessage("Getting result...");
			dialog.show();
		}
		
		@Override
		protected Void doInBackground(Void... params) {
			try {
				getResult();
			} catch (Exception e) {
				Log.e("resultPage", e.toString());
			}
			return null;
		}
		
		protected void onPostExecute(Void result) {
			dialog.dismiss();
			try {
				printResult(view);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public void getResult() throws JSONException {
		ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
		param.add(new BasicNameValuePair("tag", "getResult"));
		JSONObject jObj = jParser.getJSONObjectFromWeb("http://108.174.49.135/rewrite/api.php", param);
		
		questionId = jObj.getInt("questionid");
		answer_a = jObj.getInt("answer_a");
		answer_b = jObj.getInt("answer_b");
		answer_c = jObj.getInt("answer_c");
		total_count = jObj.getInt("totalcount");
	}
	
	public void printResult(View view) throws NumberFormatException, JSONException {

		text1.setText("" + questionId);
		text2.setText("" + answer_a + " (" + (float)answer_a / (float)total_count * 100 + "%)");
		text3.setText("" + answer_b + " (" + (float)answer_b / (float)total_count * 100 + "%)");
		text4.setText("" + answer_c + " (" + (float)answer_c / (float)total_count * 100 + "%)");
		text5.setText("" + total_count);
	}
}
